// Addin.cpp : Implementation of CAddin

#include "stdafx.h"

//#define INITGUID
//#include <objbase.h>
#define USES_IID_IMessage
#include <initguid.h>
#include <MAPIX.h>
#include <MapiUtil.h>
#include <MAPIGuid.h>
#include <MAPITags.h>
#include <mapidefs.h>

#include <vector>

//#include <boost/xpressive/xpressive.hpp>

#include "Addin.h"
#include "Settings.h"

//using namespace boost::xpressive;

std::wstring CAddin::DLLPATH = _T("0");

STDMETHODIMP CAddin::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IAddin
	};

	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

_ATL_FUNC_INFO OnSendInfo ={CC_STDCALL,VT_EMPTY,2,{VT_DISPATCH,VT_BYREF | VT_BOOL}};        
_ATL_FUNC_INFO OnOptionsAddPagesInfo={CC_STDCALL,VT_EMPTY,1,{VT_DISPATCH}};

void __stdcall CAddin::OnOptionsAddPages(IDispatch *Ctrl)
{
	CComQIPtr<Outlook::PropertyPages> spPages(Ctrl);
	ATLASSERT(spPages);
	CComVariant varProgId(OLESTR("EncryptIt.PropTab"));
	CComBSTR bstrTitle(OLESTR("EncryptIt!"));

	HRESULT hr = spPages->Add((_variant_t)varProgId,(_bstr_t)bstrTitle);
	if(FAILED(hr))
		ATLTRACE("\nFailed addin propertypage");
}

void __stdcall CAddin::OnSend(IDispatch *Obj,VARIANT_BOOL *CancelDefault)
{
	if(!m_settings.enabled)
		return;

	CComQIPtr<Outlook::_MailItem> pMi(Obj);	
	CComQIPtr<Outlook::_Inspector> insp;
	CComQIPtr<Office::_CommandBars> cmdBars;

	m_spApp->ActiveInspector(&insp);
	insp->get_CommandBars(&cmdBars);

	VARIANT_BOOL pressed;
	HRESULT msoActRes;
	msoActRes = cmdBars->GetPressedMso(CComBSTR(_T("EncryptMessage")), &pressed);

	if(msoActRes==S_OK && !pressed)
	{
		bool needToEncrypt = false;

		tstring subj, body;

		CComBSTR msgBody;
		pMi->get_Body(&msgBody);
		body = msgBody;

		CComBSTR msgSubj;
		if(m_settings.checksubj)
		{
			pMi->get_Subject(&msgSubj);
			subj = msgSubj;
		}	
		
		const tstring rule = m_settings.mask;

		//if(!m_settings.useregex) //regex support will be later, because of Xpressive does not support UNICODE directly
		//if(true)
		{
			std::vector<tstring> words;
			tokenize(rule, words, tstring(_T(",;|\\/")), true);

			std::vector<tstring>::iterator itr;
			itr = words.begin();
			int firstinbody, firstinsubj = tstring::npos;
			while(itr!=words.end())
			{
				firstinbody = body.find(*itr);
				if(!subj.empty())
					firstinsubj = subj.find(*itr);
				if(firstinbody!=tstring::npos || firstinsubj!=tstring::npos)
				{
					needToEncrypt = true;
					break;
				}
				itr++;
			}	
		}
		//else
		//{
		//	try
		//	{
		//		wsmatch wwhat;
		//		wsregex e = wsregex::compile(rule);
		//		needToEncrypt = regex_match( mb, wwhat, e );
		//
		//		//smatch swhat;
		//		//std::string hello( "hello world!" );
		//		//sregex rex = sregex::compile( "(\\w+) (\\w+)!" );				
		//		//needToEncrypt = regex_match( hello, swhat, rex );
		//
		//	}
		//	catch(...)
		//	{
		//		MessageBox(0, 
		//			_T("Incorrect regular expression was provided for mask.\n"), 
		//			_T("EncryptIt! Outlook Add-in"), 
		//			MB_OK | MB_ICONWARNING);
		//	}			
		//}

		if(needToEncrypt)
		{
			if(!m_settings.alwayson)
			{
				int userChoose = MessageBox(0, 
					_T("This message contains words which match with mask provided.\n")
					_T("Do you want to switch on encryption for this message?\n")
					_T("\"Yes\" to send encrypted,\n\"No\" to send unencrypted,\n")
					_T("\"Cancel\" to cancel Send operation and continue to edit message.\n\n")
					_T("Note: You can configure EncryptIt! on Tools->Options->EncryptIt! Property Tab.\n"), 
					_T("EncryptIt! Outlook Add-in"), 
					MB_YESNOCANCEL | MB_ICONWARNING | MB_DEFBUTTON1);
				if(userChoose==IDYES)
				{
					msoActRes = cmdBars->ExecuteMso(CComBSTR(_T("EncryptMessage")));
				}
				else if(userChoose==IDCANCEL)
				{
					*CancelDefault = true;
				}
			}
			else
			{
				msoActRes = cmdBars->ExecuteMso(CComBSTR(_T("EncryptMessage")));
			}
		}
	}	
}